package io.shuttle.mbe.api

import io.shuttle.mbe.api.annotation.ChromeMinVersion
import io.shuttle.mbe.api.types.Blob
import io.shuttle.mbe.api.types.Value1Function
import io.shuttle.mbe.api.types.Value2Function

////////////////////
// Printer Provider
////////////////////
/**
 * The `Browser.printerProvider` API exposes events used by print manager to query printers controlled by extensions, to query their capabilities and to submit print jobs to these printers.
 *
 * Permissions: "printerProvider"
 * @since Chrome 44
 */
@ChromeMinVersion(44)
interface PrinterProvider {
    // Event fired when print manager requests printer capabilities.
    // @callback printerId: String
    // @callback resultCallback: Function capabilities: Object Device capabilities in CDD format.
    val onGetCapabilityRequested: Events.Event<Value2Function<String, Value1Function<Any>>>?

    // Event fired when print manager requests printers provided by extensions.
    val onGetPrintersRequested: Events.Event<Value1Function<List<PrinterInfo>>>?

    // Event fired when print manager requests information about a USB device that may be a printer.
    //
    //Note: An application should not rely on this event being fired more than once per device.
    // If a connected device is supported it should be returned in the onGetPrintersRequested event.
    // FIXME: usb.Device
    // @callback device: USBDevice
    // @callback  resultCallback: Function printerInfo: PrinterInfo
    val onGetUsbPrinterInfoRequested: Events.Event<Value2Function<Any, Value1Function<PrinterInfo?>>>?

    // Event fired when print manager requests printing.
    val onPrintRequested: Events.Event<Value2Function<PrintJob, Value1Function<PrintError>>>?

    data class PrinterInfo(
        // Printer's human readable description.
        var description: String,
        // Unique printer ID.
        var id: String,
        // Printer's human readable name.
        var name: String,
    )

    // Error codes returned in response to onPrintRequested event.
    enum class PrintError {
        // Specifies that the operation was completed successfully.
        OK,

        // Specifies that a general failure occured.
        FAILED,

        // Specifies that the print ticket is invalid.
        // For example, the ticket is inconsistent with some capabilities,
        // or the extension is not able to handle all settings from the ticket.
        INVALID_TICKET,

        // Specifies that the document is invalid.
        // For example, data may be corrupted or the format is incompatible with the extension.
        INVALID_DATA
    }

    data class PrintJob(
        // The document content type. Supported formats are "application/pdf" and "image/pwg-raster".
        var contentType: String,
        // Blob containing the document data to print. Format must match contentType.
        var document: Blob,
        // ID of the printer which should handle the job.
        var printerId: String,
        // Print ticket in CJT format.
        //
        //The CJT reference is marked as deprecated.
        // It is deprecated for Google Cloud Print only. is not deprecated for ChromeOS printing.
        var ticket: Any,
        // The print job title.
        var title: String,
    )
}